package easy.将有序数组转换为二叉搜索树;

import java.util.LinkedList;
import java.util.Queue;

public class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        return helper(nums, 0, nums.length - 1);
    }

    private TreeNode helper(int[] nums, int left, int right) {
        if (left > right) {
            return null;
        }
        int mid = (left + right) / 2;
        TreeNode root = new TreeNode();
        root.val = nums[mid];
        root.left = helper(nums, left, mid - 1);
        root.right = helper(nums, mid + 1, right);
        return root;
    }


    public static void main(String[] args) {
        int[] nums = {-10, -3, 0, 5, 9};
        int[] nums2 = {1, 3};
        Solution solution = new Solution();
        System.out.println(solution.sortedArrayToBST(nums));
        System.out.println(solution.sortedArrayToBST(nums2));
    }
}
